package org.zyl.util.validate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证工具类
 * @author yzzhouyalei@foxmail.com
 * @date 2016年11月17日下午9:55:40
 */
public class ValidateUtil {
	public static String pattern = "^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
	/**
	 * 手机号码验证
	 * @param mobile
	 * @return 正确/错误 ；true/false
	 */
	public static boolean isMobile(String mobile){
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(mobile);
		return m.find();
	}
	/**
	 * email 验证
	 * @param email
	 * @return 正确/错误 ；true/false
	 */
	public static boolean  isEmail(String email){
		Pattern regex = Pattern.compile("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");
		if(ValidateUtil.isNotBlank(email.trim())){
			return regex.matcher(email).matches();
		}else{
			return false;
		}
	}
	
	public static boolean isNotBlank(String key){
		if (key!=null&&!"".equals(key)&&!"".equals(key.trim())&&!"null".equals(key)) {
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean isDateTime(String time){
		Pattern pt = Pattern.compile("^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$");
		return ValidateUtil.isNotBlank(time)?pt.matcher(time).matches():false;
	}
	
	public static boolean isIp(String ip){
		Pattern pt = Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}");
		return ValidateUtil.isNotBlank(ip)?pt.matcher(ip).matches():false;
	}

	/**
	 * 是否数字  true是,false不是
	 * @author yzzhouyalei@foxmail.com
	 * @time 2017-04-14 17:06:32
	 */
	public static boolean isNumeric(String str) {
		if (!isNotBlank(str)) {
			return false;
		}
		
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
	
	public static boolean isHTMLSpecialChar(String str){
		return str.indexOf("<")!=-1||str.indexOf(">")!=-1;
	}
	/**
	 * 是否json, true是,false不是
	 * @author yzzhouyalei@foxmail.com
	 * @time 2018-03-08 10:59:42
	 */
	public static boolean isJson(String str) {
		return new JsonValidator().validate(str);
	}
}